package com.syp.test.clustercacheflush.handler.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.syp.test.clustercacheflush.domain.TaskEntity;
import com.syp.test.clustercacheflush.handler.TaskProcessHandler;
import com.syp.test.clustercacheflush.util.zkflushcache.ZkCacheSyncNotify;
import com.syp.test.clustercacheflush.util.zkflushcache.handler.CacheSyncNotifyMater;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ClusterCacheSyncNotifyHandler implements TaskProcessHandler {

	@Autowired
	private CacheSyncNotifyMater cacheSyncNotifyMater;

	@Override
	public boolean process(TaskEntity task) {
		try {
			JSONObject json = JSON.parseObject(task.getTaskData());
			ZkCacheSyncNotify zkNotify = JSON.toJavaObject(json, ZkCacheSyncNotify.class);
			this.cacheSyncNotifyMater.writeCacheSyncNotify(zkNotify);
		} catch (RuntimeException e) {
			log.error("执行集群缓存同步通知任务异常。", e);
			return false;
		}
		return true;
	}

}
